package org.dromara.chicken.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.chicken.domain.ChickenChickIn;
import org.dromara.chicken.service.IChickenChickInService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 鸡苗采购
 *
 * @author Mr Lei QQ:235921049
 * @date 2024-06-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/farm/chickenChickIn")
public class ChickenChickInController extends BaseController {

    private final IChickenChickInService chickenChickInService;

    /**
     * 查询鸡苗采购列表
     */
    @SaCheckPermission("chicken:chickenChickIn:list")
    @GetMapping("/list")
    public TableDataInfo<ChickenChickIn> list(ChickenChickIn bo, PageQuery pageQuery) {
        return chickenChickInService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出鸡苗采购列表
     */
    @SaCheckPermission("chicken:chickenChickIn:export")
    @Log(title = "鸡苗采购", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(ChickenChickIn bo, HttpServletResponse response) {
        List<ChickenChickIn> list = chickenChickInService.queryList(bo);
        ExcelUtil.exportExcel(list, "鸡苗采购", ChickenChickIn.class, response);
    }

    /**
     * 获取鸡苗采购详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("chicken:chickenChickIn:query")
    @GetMapping("/{id}")
    public R<ChickenChickIn> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(chickenChickInService.queryById(id));
    }

    /**
     * 新增鸡苗采购
     */
    @SaCheckPermission("chicken:chickenChickIn:add")
    @Log(title = "鸡苗采购", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody ChickenChickIn bo) {
        return toAjax(chickenChickInService.insertByBo(bo));
    }

    /**
     * 修改鸡苗采购
     */
    @SaCheckPermission("chicken:chickenChickIn:edit")
    @Log(title = "鸡苗采购", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody ChickenChickIn bo) {
        return toAjax(chickenChickInService.updateByBo(bo));
    }

    /**
     * 删除鸡苗采购
     *
     * @param ids 主键串
     */
    @SaCheckPermission("chicken:chickenChickIn:remove")
    @Log(title = "鸡苗采购", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(chickenChickInService.deleteWithValidByIds(List.of(ids), true));
    }
}
